{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-05T08:14:06.191186100Z",
     "start_time": "2024-06-05T08:14:00.926929500Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "from torch import optim\n",
    "from datasets import data_loader, text_ClS\n",
    "from config import Config\n",
    "from model import Model\n",
    "cfg = Config()\n",
    "data_path = \"sources/weibo_senti_100k.csv\" #训练集\n",
    "data_stop_path = \"sources/hit_stopword.txt\" #停用词\n",
    "dict_path = \"sources/dict\"  #词典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-05T08:15:59.383511500Z",
     "start_time": "2024-06-05T08:14:29.078199Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Dumping model to file cache C:\\Users\\admin\\AppData\\Local\\Temp\\jieba.cache\n",
      "Loading model cost 0.727 seconds.\n",
      "Prefix dict has been built successfully.\n"
     ]
    }
   ],
   "source": [
    "#获取数据集\n",
    "dataset = text_ClS(dict_path, data_path, data_stop_path) #全量的微博数据集\n",
    "train_dataloader = data_loader(dataset, cfg) #batch_size generator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-05T08:16:27.647545900Z",
     "start_time": "2024-06-05T08:16:21.159700900Z"
    }
   },
   "outputs": [],
   "source": [
    "#实例化模型\n",
    "model_text_cls = Model(cfg)\n",
    "model_text_cls.to(cfg.device) #to()函数指定你的模型在什么设备运行\n",
    "loss_func = nn.CrossEntropyLoss() #损失函数\n",
    "optimizer = optim.Adam(model_text_cls.parameters(), lr=cfg.lr) #优化器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-05T08:36:15.349206800Z",
     "start_time": "2024-06-05T08:16:28.930816900Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\admin\\AppData\\Local\\Temp\\ipykernel_7016\\3645490065.py:5: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
      "  data = torch.tensor(data.long()).to(cfg.device)\n",
      "C:\\Users\\admin\\AppData\\Local\\Temp\\ipykernel_7016\\3645490065.py:6: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
      "  label = torch.tensor(label, dtype=torch.int64).to(cfg.device)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch is:0, iter is:0, loss_val is: 0.6992724537849426\n",
      "epoch is:0, iter is:100, loss_val is: 0.693699300289154\n",
      "epoch is:0, iter is:200, loss_val is: 0.6928731799125671\n",
      "epoch is:0, iter is:300, loss_val is: 0.6926670074462891\n",
      "epoch is:0, iter is:400, loss_val is: 0.6945147514343262\n",
      "epoch is:1, iter is:0, loss_val is: 0.6960762739181519\n",
      "epoch is:1, iter is:100, loss_val is: 0.6948328018188477\n",
      "epoch is:1, iter is:200, loss_val is: 0.6967808604240417\n",
      "epoch is:1, iter is:300, loss_val is: 0.6930256485939026\n",
      "epoch is:1, iter is:400, loss_val is: 0.6926717758178711\n",
      "epoch is:2, iter is:0, loss_val is: 0.694124698638916\n",
      "epoch is:2, iter is:100, loss_val is: 0.6932501196861267\n",
      "epoch is:2, iter is:200, loss_val is: 0.6924666166305542\n",
      "epoch is:2, iter is:300, loss_val is: 0.6936075687408447\n",
      "epoch is:2, iter is:400, loss_val is: 0.6931210160255432\n",
      "epoch is:3, iter is:0, loss_val is: 0.6926794648170471\n",
      "epoch is:3, iter is:100, loss_val is: 0.6929075121879578\n",
      "epoch is:3, iter is:200, loss_val is: 0.6929888129234314\n",
      "epoch is:3, iter is:300, loss_val is: 0.6928356885910034\n",
      "epoch is:3, iter is:400, loss_val is: 0.6925507187843323\n",
      "epoch is:4, iter is:0, loss_val is: 0.6942986845970154\n",
      "epoch is:4, iter is:100, loss_val is: 0.6925793290138245\n",
      "epoch is:4, iter is:200, loss_val is: 0.6930158138275146\n",
      "epoch is:4, iter is:300, loss_val is: 0.692775309085846\n",
      "epoch is:4, iter is:400, loss_val is: 0.6933303475379944\n",
      "epoch is:5, iter is:0, loss_val is: 0.6938159465789795\n",
      "epoch is:5, iter is:100, loss_val is: 0.6935123801231384\n",
      "epoch is:5, iter is:200, loss_val is: 0.6931179761886597\n",
      "epoch is:5, iter is:300, loss_val is: 0.6939812898635864\n",
      "epoch is:5, iter is:400, loss_val is: 0.6934370994567871\n",
      "epoch is:6, iter is:0, loss_val is: 0.6934481263160706\n",
      "epoch is:6, iter is:100, loss_val is: 0.6933392286300659\n",
      "epoch is:6, iter is:200, loss_val is: 0.6924226880073547\n",
      "epoch is:6, iter is:300, loss_val is: 0.6929758191108704\n",
      "epoch is:6, iter is:400, loss_val is: 0.6927652359008789\n",
      "epoch is:7, iter is:0, loss_val is: 0.6931424140930176\n",
      "epoch is:7, iter is:100, loss_val is: 0.6933509707450867\n",
      "epoch is:7, iter is:200, loss_val is: 0.6938871741294861\n",
      "epoch is:7, iter is:300, loss_val is: 0.6935403943061829\n",
      "epoch is:7, iter is:400, loss_val is: 0.6931098699569702\n",
      "epoch is:8, iter is:0, loss_val is: 0.6929500102996826\n",
      "epoch is:8, iter is:100, loss_val is: 0.6927779912948608\n",
      "epoch is:8, iter is:200, loss_val is: 0.6933193206787109\n",
      "epoch is:8, iter is:300, loss_val is: 0.6935696601867676\n",
      "epoch is:8, iter is:400, loss_val is: 0.6932321786880493\n",
      "epoch is:9, iter is:0, loss_val is: 0.6940579414367676\n",
      "epoch is:9, iter is:100, loss_val is: 0.6931208372116089\n",
      "epoch is:9, iter is:200, loss_val is: 0.6933280825614929\n",
      "epoch is:9, iter is:300, loss_val is: 0.6934446692466736\n",
      "epoch is:9, iter is:400, loss_val is: 0.6938192248344421\n"
     ]
    }
   ],
   "source": [
    "#模型的训练\n",
    "for epoch in range(cfg.num_epochs):\n",
    "    for i, batch in enumerate(train_dataloader):\n",
    "        label, data = batch\n",
    "        data = torch.tensor(data.long()).to(cfg.device)\n",
    "        label = torch.tensor(label, dtype=torch.int64).to(cfg.device)\n",
    "        pred = model_text_cls.forward(data) #模型的输出值\n",
    "        loss_val = loss_func(pred, label)\n",
    "        \n",
    "        optimizer.zero_grad() #梯度清零\n",
    "        loss_val.backward()\n",
    "        optimizer.step()\n",
    "        \n",
    "        if i % 100 == 0:\n",
    "            print(\"epoch is:{}, iter is:{}, loss_val is: {}\".format(epoch, i, loss_val)) #每100个iter输出一次损失值\n",
    "    \n",
    "    if epoch % 10 == 0:\n",
    "        torch.save(model_text_cls.state_dict(), \"models/{}.pth\".format(epoch))\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-05T08:41:43.025484Z",
     "start_time": "2024-06-05T08:41:42.932347800Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "<All keys matched successfully>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#模型推理\n",
    "model_text_cls.load_state_dict(torch.load(\"models/0.pth\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-05T08:42:42.437411400Z",
     "start_time": "2024-06-05T08:41:52.923681500Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\admin\\AppData\\Local\\Temp\\ipykernel_7016\\620770965.py:3: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
      "  data = torch.tensor(data.long()).to(cfg.device)\n",
      "C:\\Users\\admin\\AppData\\Local\\Temp\\ipykernel_7016\\620770965.py:4: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
      "  label = torch.tensor(label, dtype=torch.int64).to(cfg.device)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iter is:0, accuracy is:0.48046875\n",
      "iter is:1, accuracy is:0.5078125\n",
      "iter is:2, accuracy is:0.4765625\n",
      "iter is:3, accuracy is:0.4921875\n",
      "iter is:4, accuracy is:0.50390625\n",
      "iter is:5, accuracy is:0.5390625\n",
      "iter is:6, accuracy is:0.48828125\n",
      "iter is:7, accuracy is:0.515625\n",
      "iter is:8, accuracy is:0.4453125\n",
      "iter is:9, accuracy is:0.53515625\n",
      "iter is:10, accuracy is:0.484375\n",
      "iter is:11, accuracy is:0.51953125\n",
      "iter is:12, accuracy is:0.5078125\n",
      "iter is:13, accuracy is:0.50390625\n",
      "iter is:14, accuracy is:0.51171875\n",
      "iter is:15, accuracy is:0.5\n",
      "iter is:16, accuracy is:0.5703125\n",
      "iter is:17, accuracy is:0.54296875\n",
      "iter is:18, accuracy is:0.47265625\n",
      "iter is:19, accuracy is:0.53125\n",
      "iter is:20, accuracy is:0.4609375\n",
      "iter is:21, accuracy is:0.484375\n",
      "iter is:22, accuracy is:0.51171875\n",
      "iter is:23, accuracy is:0.46875\n",
      "iter is:24, accuracy is:0.46875\n",
      "iter is:25, accuracy is:0.48828125\n",
      "iter is:26, accuracy is:0.5234375\n",
      "iter is:27, accuracy is:0.5\n",
      "iter is:28, accuracy is:0.47265625\n",
      "iter is:29, accuracy is:0.4765625\n",
      "iter is:30, accuracy is:0.5078125\n",
      "iter is:31, accuracy is:0.5\n",
      "iter is:32, accuracy is:0.46875\n",
      "iter is:33, accuracy is:0.45703125\n",
      "iter is:34, accuracy is:0.49609375\n",
      "iter is:35, accuracy is:0.48046875\n",
      "iter is:36, accuracy is:0.546875\n",
      "iter is:37, accuracy is:0.48046875\n",
      "iter is:38, accuracy is:0.51171875\n",
      "iter is:39, accuracy is:0.54296875\n",
      "iter is:40, accuracy is:0.48046875\n",
      "iter is:41, accuracy is:0.5078125\n",
      "iter is:42, accuracy is:0.50390625\n",
      "iter is:43, accuracy is:0.5234375\n",
      "iter is:44, accuracy is:0.56640625\n",
      "iter is:45, accuracy is:0.48046875\n",
      "iter is:46, accuracy is:0.484375\n",
      "iter is:47, accuracy is:0.5\n",
      "iter is:48, accuracy is:0.47265625\n",
      "iter is:49, accuracy is:0.44140625\n",
      "iter is:50, accuracy is:0.5\n",
      "iter is:51, accuracy is:0.48046875\n",
      "iter is:52, accuracy is:0.546875\n",
      "iter is:53, accuracy is:0.54296875\n",
      "iter is:54, accuracy is:0.53515625\n",
      "iter is:55, accuracy is:0.4921875\n",
      "iter is:56, accuracy is:0.60546875\n",
      "iter is:57, accuracy is:0.5078125\n",
      "iter is:58, accuracy is:0.47265625\n",
      "iter is:59, accuracy is:0.515625\n",
      "iter is:60, accuracy is:0.484375\n",
      "iter is:61, accuracy is:0.5234375\n",
      "iter is:62, accuracy is:0.46875\n",
      "iter is:63, accuracy is:0.5234375\n",
      "iter is:64, accuracy is:0.45703125\n",
      "iter is:65, accuracy is:0.5078125\n",
      "iter is:66, accuracy is:0.5078125\n",
      "iter is:67, accuracy is:0.47265625\n",
      "iter is:68, accuracy is:0.45703125\n",
      "iter is:69, accuracy is:0.484375\n",
      "iter is:70, accuracy is:0.6015625\n",
      "iter is:71, accuracy is:0.4921875\n",
      "iter is:72, accuracy is:0.58203125\n",
      "iter is:73, accuracy is:0.46875\n",
      "iter is:74, accuracy is:0.51953125\n",
      "iter is:75, accuracy is:0.49609375\n",
      "iter is:76, accuracy is:0.49609375\n",
      "iter is:77, accuracy is:0.48046875\n",
      "iter is:78, accuracy is:0.484375\n",
      "iter is:79, accuracy is:0.53515625\n",
      "iter is:80, accuracy is:0.49609375\n",
      "iter is:81, accuracy is:0.46484375\n",
      "iter is:82, accuracy is:0.5078125\n",
      "iter is:83, accuracy is:0.5078125\n",
      "iter is:84, accuracy is:0.484375\n",
      "iter is:85, accuracy is:0.484375\n",
      "iter is:86, accuracy is:0.578125\n",
      "iter is:87, accuracy is:0.53125\n",
      "iter is:88, accuracy is:0.53515625\n",
      "iter is:89, accuracy is:0.47265625\n",
      "iter is:90, accuracy is:0.50390625\n",
      "iter is:91, accuracy is:0.4453125\n",
      "iter is:92, accuracy is:0.43359375\n",
      "iter is:93, accuracy is:0.46484375\n",
      "iter is:94, accuracy is:0.484375\n",
      "iter is:95, accuracy is:0.4765625\n",
      "iter is:96, accuracy is:0.48828125\n",
      "iter is:97, accuracy is:0.44921875\n",
      "iter is:98, accuracy is:0.5390625\n",
      "iter is:99, accuracy is:0.48046875\n",
      "iter is:100, accuracy is:0.4921875\n",
      "iter is:101, accuracy is:0.50390625\n",
      "iter is:102, accuracy is:0.50390625\n",
      "iter is:103, accuracy is:0.46484375\n",
      "iter is:104, accuracy is:0.453125\n",
      "iter is:105, accuracy is:0.515625\n",
      "iter is:106, accuracy is:0.4921875\n",
      "iter is:107, accuracy is:0.48828125\n",
      "iter is:108, accuracy is:0.50390625\n",
      "iter is:109, accuracy is:0.49609375\n",
      "iter is:110, accuracy is:0.5703125\n",
      "iter is:111, accuracy is:0.5078125\n",
      "iter is:112, accuracy is:0.48046875\n",
      "iter is:113, accuracy is:0.5703125\n",
      "iter is:114, accuracy is:0.4921875\n",
      "iter is:115, accuracy is:0.55078125\n",
      "iter is:116, accuracy is:0.4765625\n",
      "iter is:117, accuracy is:0.421875\n",
      "iter is:118, accuracy is:0.53515625\n",
      "iter is:119, accuracy is:0.51171875\n",
      "iter is:120, accuracy is:0.4609375\n",
      "iter is:121, accuracy is:0.50390625\n",
      "iter is:122, accuracy is:0.5\n",
      "iter is:123, accuracy is:0.48046875\n",
      "iter is:124, accuracy is:0.48828125\n",
      "iter is:125, accuracy is:0.515625\n",
      "iter is:126, accuracy is:0.5\n",
      "iter is:127, accuracy is:0.53515625\n",
      "iter is:128, accuracy is:0.5\n",
      "iter is:129, accuracy is:0.48828125\n",
      "iter is:130, accuracy is:0.42578125\n",
      "iter is:131, accuracy is:0.5390625\n",
      "iter is:132, accuracy is:0.4375\n",
      "iter is:133, accuracy is:0.47265625\n",
      "iter is:134, accuracy is:0.5078125\n",
      "iter is:135, accuracy is:0.44140625\n",
      "iter is:136, accuracy is:0.5078125\n",
      "iter is:137, accuracy is:0.51171875\n",
      "iter is:138, accuracy is:0.5546875\n",
      "iter is:139, accuracy is:0.51171875\n",
      "iter is:140, accuracy is:0.51171875\n",
      "iter is:141, accuracy is:0.5\n",
      "iter is:142, accuracy is:0.515625\n",
      "iter is:143, accuracy is:0.46484375\n",
      "iter is:144, accuracy is:0.51171875\n",
      "iter is:145, accuracy is:0.48046875\n",
      "iter is:146, accuracy is:0.546875\n",
      "iter is:147, accuracy is:0.5234375\n",
      "iter is:148, accuracy is:0.48828125\n",
      "iter is:149, accuracy is:0.49609375\n",
      "iter is:150, accuracy is:0.46875\n",
      "iter is:151, accuracy is:0.53515625\n",
      "iter is:152, accuracy is:0.5390625\n",
      "iter is:153, accuracy is:0.48046875\n",
      "iter is:154, accuracy is:0.53515625\n",
      "iter is:155, accuracy is:0.48046875\n",
      "iter is:156, accuracy is:0.45703125\n",
      "iter is:157, accuracy is:0.51171875\n",
      "iter is:158, accuracy is:0.5234375\n",
      "iter is:159, accuracy is:0.44140625\n",
      "iter is:160, accuracy is:0.46875\n",
      "iter is:161, accuracy is:0.54296875\n",
      "iter is:162, accuracy is:0.52734375\n",
      "iter is:163, accuracy is:0.47265625\n",
      "iter is:164, accuracy is:0.49609375\n",
      "iter is:165, accuracy is:0.4921875\n",
      "iter is:166, accuracy is:0.4921875\n",
      "iter is:167, accuracy is:0.52734375\n",
      "iter is:168, accuracy is:0.51171875\n",
      "iter is:169, accuracy is:0.47265625\n",
      "iter is:170, accuracy is:0.5\n",
      "iter is:171, accuracy is:0.54296875\n",
      "iter is:172, accuracy is:0.51953125\n",
      "iter is:173, accuracy is:0.53515625\n",
      "iter is:174, accuracy is:0.52734375\n",
      "iter is:175, accuracy is:0.5\n",
      "iter is:176, accuracy is:0.48046875\n",
      "iter is:177, accuracy is:0.52734375\n",
      "iter is:178, accuracy is:0.49609375\n",
      "iter is:179, accuracy is:0.515625\n",
      "iter is:180, accuracy is:0.48046875\n",
      "iter is:181, accuracy is:0.52734375\n",
      "iter is:182, accuracy is:0.4609375\n",
      "iter is:183, accuracy is:0.48046875\n",
      "iter is:184, accuracy is:0.51953125\n",
      "iter is:185, accuracy is:0.53125\n",
      "iter is:186, accuracy is:0.50390625\n",
      "iter is:187, accuracy is:0.48046875\n",
      "iter is:188, accuracy is:0.5078125\n",
      "iter is:189, accuracy is:0.5078125\n",
      "iter is:190, accuracy is:0.5234375\n",
      "iter is:191, accuracy is:0.47265625\n",
      "iter is:192, accuracy is:0.51953125\n",
      "iter is:193, accuracy is:0.45703125\n",
      "iter is:194, accuracy is:0.5078125\n",
      "iter is:195, accuracy is:0.53125\n",
      "iter is:196, accuracy is:0.5\n",
      "iter is:197, accuracy is:0.54296875\n",
      "iter is:198, accuracy is:0.5625\n",
      "iter is:199, accuracy is:0.51953125\n",
      "iter is:200, accuracy is:0.48046875\n",
      "iter is:201, accuracy is:0.5078125\n",
      "iter is:202, accuracy is:0.53125\n",
      "iter is:203, accuracy is:0.52734375\n",
      "iter is:204, accuracy is:0.54296875\n",
      "iter is:205, accuracy is:0.5\n",
      "iter is:206, accuracy is:0.48828125\n",
      "iter is:207, accuracy is:0.5390625\n",
      "iter is:208, accuracy is:0.48828125\n",
      "iter is:209, accuracy is:0.4453125\n",
      "iter is:210, accuracy is:0.484375\n",
      "iter is:211, accuracy is:0.4921875\n",
      "iter is:212, accuracy is:0.46484375\n",
      "iter is:213, accuracy is:0.5234375\n",
      "iter is:214, accuracy is:0.47265625\n",
      "iter is:215, accuracy is:0.52734375\n",
      "iter is:216, accuracy is:0.51171875\n",
      "iter is:217, accuracy is:0.53515625\n",
      "iter is:218, accuracy is:0.5\n",
      "iter is:219, accuracy is:0.5546875\n",
      "iter is:220, accuracy is:0.56640625\n",
      "iter is:221, accuracy is:0.52734375\n",
      "iter is:222, accuracy is:0.48828125\n",
      "iter is:223, accuracy is:0.53125\n",
      "iter is:224, accuracy is:0.5\n",
      "iter is:225, accuracy is:0.54296875\n",
      "iter is:226, accuracy is:0.515625\n",
      "iter is:227, accuracy is:0.48046875\n",
      "iter is:228, accuracy is:0.46875\n",
      "iter is:229, accuracy is:0.4453125\n",
      "iter is:230, accuracy is:0.546875\n",
      "iter is:231, accuracy is:0.53125\n",
      "iter is:232, accuracy is:0.46875\n",
      "iter is:233, accuracy is:0.51171875\n",
      "iter is:234, accuracy is:0.45703125\n",
      "iter is:235, accuracy is:0.54296875\n",
      "iter is:236, accuracy is:0.49609375\n",
      "iter is:237, accuracy is:0.48828125\n",
      "iter is:238, accuracy is:0.53125\n",
      "iter is:239, accuracy is:0.51171875\n",
      "iter is:240, accuracy is:0.49609375\n",
      "iter is:241, accuracy is:0.4921875\n",
      "iter is:242, accuracy is:0.4921875\n",
      "iter is:243, accuracy is:0.43359375\n",
      "iter is:244, accuracy is:0.4765625\n",
      "iter is:245, accuracy is:0.48828125\n",
      "iter is:246, accuracy is:0.5078125\n",
      "iter is:247, accuracy is:0.53515625\n",
      "iter is:248, accuracy is:0.4375\n",
      "iter is:249, accuracy is:0.51953125\n",
      "iter is:250, accuracy is:0.5078125\n",
      "iter is:251, accuracy is:0.50390625\n",
      "iter is:252, accuracy is:0.4921875\n",
      "iter is:253, accuracy is:0.42578125\n",
      "iter is:254, accuracy is:0.5234375\n",
      "iter is:255, accuracy is:0.5\n",
      "iter is:256, accuracy is:0.4296875\n",
      "iter is:257, accuracy is:0.46484375\n",
      "iter is:258, accuracy is:0.53125\n",
      "iter is:259, accuracy is:0.49609375\n",
      "iter is:260, accuracy is:0.5\n",
      "iter is:261, accuracy is:0.5390625\n",
      "iter is:262, accuracy is:0.52734375\n",
      "iter is:263, accuracy is:0.53125\n",
      "iter is:264, accuracy is:0.5078125\n",
      "iter is:265, accuracy is:0.53125\n",
      "iter is:266, accuracy is:0.48828125\n",
      "iter is:267, accuracy is:0.58984375\n",
      "iter is:268, accuracy is:0.484375\n",
      "iter is:269, accuracy is:0.4921875\n",
      "iter is:270, accuracy is:0.4921875\n",
      "iter is:271, accuracy is:0.5546875\n",
      "iter is:272, accuracy is:0.5\n",
      "iter is:273, accuracy is:0.4921875\n",
      "iter is:274, accuracy is:0.48828125\n",
      "iter is:275, accuracy is:0.4375\n",
      "iter is:276, accuracy is:0.49609375\n",
      "iter is:277, accuracy is:0.515625\n",
      "iter is:278, accuracy is:0.5\n",
      "iter is:279, accuracy is:0.515625\n",
      "iter is:280, accuracy is:0.49609375\n",
      "iter is:281, accuracy is:0.4609375\n",
      "iter is:282, accuracy is:0.484375\n",
      "iter is:283, accuracy is:0.51953125\n",
      "iter is:284, accuracy is:0.41796875\n",
      "iter is:285, accuracy is:0.50390625\n",
      "iter is:286, accuracy is:0.5\n",
      "iter is:287, accuracy is:0.5234375\n",
      "iter is:288, accuracy is:0.515625\n",
      "iter is:289, accuracy is:0.50390625\n",
      "iter is:290, accuracy is:0.49609375\n",
      "iter is:291, accuracy is:0.4453125\n",
      "iter is:292, accuracy is:0.48046875\n",
      "iter is:293, accuracy is:0.53125\n",
      "iter is:294, accuracy is:0.49609375\n",
      "iter is:295, accuracy is:0.48828125\n",
      "iter is:296, accuracy is:0.5078125\n",
      "iter is:297, accuracy is:0.5\n",
      "iter is:298, accuracy is:0.47265625\n",
      "iter is:299, accuracy is:0.515625\n",
      "iter is:300, accuracy is:0.5234375\n",
      "iter is:301, accuracy is:0.48046875\n",
      "iter is:302, accuracy is:0.5\n",
      "iter is:303, accuracy is:0.4765625\n",
      "iter is:304, accuracy is:0.48828125\n",
      "iter is:305, accuracy is:0.51171875\n",
      "iter is:306, accuracy is:0.48828125\n",
      "iter is:307, accuracy is:0.53515625\n",
      "iter is:308, accuracy is:0.54296875\n",
      "iter is:309, accuracy is:0.4921875\n",
      "iter is:310, accuracy is:0.5078125\n",
      "iter is:311, accuracy is:0.5234375\n",
      "iter is:312, accuracy is:0.45703125\n",
      "iter is:313, accuracy is:0.453125\n",
      "iter is:314, accuracy is:0.51953125\n",
      "iter is:315, accuracy is:0.484375\n",
      "iter is:316, accuracy is:0.47265625\n",
      "iter is:317, accuracy is:0.48046875\n",
      "iter is:318, accuracy is:0.4921875\n",
      "iter is:319, accuracy is:0.5546875\n",
      "iter is:320, accuracy is:0.484375\n",
      "iter is:321, accuracy is:0.4921875\n",
      "iter is:322, accuracy is:0.53125\n",
      "iter is:323, accuracy is:0.48828125\n",
      "iter is:324, accuracy is:0.51953125\n",
      "iter is:325, accuracy is:0.5078125\n",
      "iter is:326, accuracy is:0.484375\n",
      "iter is:327, accuracy is:0.51953125\n",
      "iter is:328, accuracy is:0.48046875\n",
      "iter is:329, accuracy is:0.51171875\n",
      "iter is:330, accuracy is:0.4375\n",
      "iter is:331, accuracy is:0.45703125\n",
      "iter is:332, accuracy is:0.5\n",
      "iter is:333, accuracy is:0.54296875\n",
      "iter is:334, accuracy is:0.46875\n",
      "iter is:335, accuracy is:0.5546875\n",
      "iter is:336, accuracy is:0.5\n",
      "iter is:337, accuracy is:0.5546875\n",
      "iter is:338, accuracy is:0.45703125\n",
      "iter is:339, accuracy is:0.5078125\n",
      "iter is:340, accuracy is:0.48828125\n",
      "iter is:341, accuracy is:0.5078125\n",
      "iter is:342, accuracy is:0.51171875\n",
      "iter is:343, accuracy is:0.5\n",
      "iter is:344, accuracy is:0.46484375\n",
      "iter is:345, accuracy is:0.47265625\n",
      "iter is:346, accuracy is:0.47265625\n",
      "iter is:347, accuracy is:0.50390625\n",
      "iter is:348, accuracy is:0.53515625\n",
      "iter is:349, accuracy is:0.4765625\n",
      "iter is:350, accuracy is:0.43359375\n",
      "iter is:351, accuracy is:0.5078125\n",
      "iter is:352, accuracy is:0.48046875\n",
      "iter is:353, accuracy is:0.55078125\n",
      "iter is:354, accuracy is:0.453125\n",
      "iter is:355, accuracy is:0.54296875\n",
      "iter is:356, accuracy is:0.5\n",
      "iter is:357, accuracy is:0.51953125\n",
      "iter is:358, accuracy is:0.50390625\n",
      "iter is:359, accuracy is:0.53125\n",
      "iter is:360, accuracy is:0.46875\n",
      "iter is:361, accuracy is:0.48828125\n",
      "iter is:362, accuracy is:0.5078125\n",
      "iter is:363, accuracy is:0.5\n",
      "iter is:364, accuracy is:0.47265625\n",
      "iter is:365, accuracy is:0.48828125\n",
      "iter is:366, accuracy is:0.515625\n",
      "iter is:367, accuracy is:0.5\n",
      "iter is:368, accuracy is:0.45703125\n",
      "iter is:369, accuracy is:0.453125\n",
      "iter is:370, accuracy is:0.5078125\n",
      "iter is:371, accuracy is:0.48828125\n",
      "iter is:372, accuracy is:0.4921875\n",
      "iter is:373, accuracy is:0.5\n",
      "iter is:374, accuracy is:0.4765625\n",
      "iter is:375, accuracy is:0.44921875\n",
      "iter is:376, accuracy is:0.453125\n",
      "iter is:377, accuracy is:0.484375\n",
      "iter is:378, accuracy is:0.45703125\n",
      "iter is:379, accuracy is:0.5546875\n",
      "iter is:380, accuracy is:0.46484375\n",
      "iter is:381, accuracy is:0.4765625\n",
      "iter is:382, accuracy is:0.484375\n",
      "iter is:383, accuracy is:0.51171875\n",
      "iter is:384, accuracy is:0.48828125\n",
      "iter is:385, accuracy is:0.5234375\n",
      "iter is:386, accuracy is:0.546875\n",
      "iter is:387, accuracy is:0.484375\n",
      "iter is:388, accuracy is:0.53125\n",
      "iter is:389, accuracy is:0.4921875\n",
      "iter is:390, accuracy is:0.46875\n",
      "iter is:391, accuracy is:0.5234375\n",
      "iter is:392, accuracy is:0.46484375\n",
      "iter is:393, accuracy is:0.52734375\n",
      "iter is:394, accuracy is:0.5390625\n",
      "iter is:395, accuracy is:0.53125\n",
      "iter is:396, accuracy is:0.46484375\n",
      "iter is:397, accuracy is:0.46484375\n",
      "iter is:398, accuracy is:0.515625\n",
      "iter is:399, accuracy is:0.484375\n",
      "iter is:400, accuracy is:0.4765625\n",
      "iter is:401, accuracy is:0.4921875\n",
      "iter is:402, accuracy is:0.515625\n",
      "iter is:403, accuracy is:0.46875\n",
      "iter is:404, accuracy is:0.48046875\n",
      "iter is:405, accuracy is:0.53125\n",
      "iter is:406, accuracy is:0.50390625\n",
      "iter is:407, accuracy is:0.50390625\n",
      "iter is:408, accuracy is:0.51953125\n",
      "iter is:409, accuracy is:0.49609375\n",
      "iter is:410, accuracy is:0.4609375\n",
      "iter is:411, accuracy is:0.54296875\n",
      "iter is:412, accuracy is:0.49609375\n",
      "iter is:413, accuracy is:0.51171875\n",
      "iter is:414, accuracy is:0.5\n",
      "iter is:415, accuracy is:0.484375\n",
      "iter is:416, accuracy is:0.53125\n",
      "iter is:417, accuracy is:0.5234375\n",
      "iter is:418, accuracy is:0.45703125\n",
      "iter is:419, accuracy is:0.53515625\n",
      "iter is:420, accuracy is:0.49609375\n",
      "iter is:421, accuracy is:0.5234375\n",
      "iter is:422, accuracy is:0.5390625\n",
      "iter is:423, accuracy is:0.51953125\n",
      "iter is:424, accuracy is:0.46484375\n",
      "iter is:425, accuracy is:0.48046875\n",
      "iter is:426, accuracy is:0.48828125\n",
      "iter is:427, accuracy is:0.51953125\n",
      "iter is:428, accuracy is:0.48828125\n",
      "iter is:429, accuracy is:0.515625\n",
      "iter is:430, accuracy is:0.48046875\n",
      "iter is:431, accuracy is:0.47265625\n",
      "iter is:432, accuracy is:0.50390625\n",
      "iter is:433, accuracy is:0.55078125\n",
      "iter is:434, accuracy is:0.47265625\n",
      "iter is:435, accuracy is:0.4921875\n",
      "iter is:436, accuracy is:0.55078125\n",
      "iter is:437, accuracy is:0.5078125\n",
      "iter is:438, accuracy is:0.48828125\n",
      "iter is:439, accuracy is:0.453125\n",
      "iter is:440, accuracy is:0.51171875\n",
      "iter is:441, accuracy is:0.48828125\n",
      "iter is:442, accuracy is:0.50390625\n",
      "iter is:443, accuracy is:0.4921875\n",
      "iter is:444, accuracy is:0.5625\n",
      "iter is:445, accuracy is:0.47265625\n",
      "iter is:446, accuracy is:0.51171875\n",
      "iter is:447, accuracy is:0.4453125\n",
      "iter is:448, accuracy is:0.51953125\n",
      "iter is:449, accuracy is:0.51953125\n",
      "iter is:450, accuracy is:0.47265625\n",
      "iter is:451, accuracy is:0.4921875\n",
      "iter is:452, accuracy is:0.515625\n",
      "iter is:453, accuracy is:0.453125\n",
      "iter is:454, accuracy is:0.50390625\n",
      "iter is:455, accuracy is:0.5234375\n",
      "iter is:456, accuracy is:0.49609375\n",
      "iter is:457, accuracy is:0.52734375\n",
      "iter is:458, accuracy is:0.53125\n",
      "iter is:459, accuracy is:0.52734375\n",
      "iter is:460, accuracy is:0.5078125\n",
      "iter is:461, accuracy is:0.4921875\n",
      "iter is:462, accuracy is:0.453125\n",
      "iter is:463, accuracy is:0.47265625\n",
      "iter is:464, accuracy is:0.47265625\n",
      "iter is:465, accuracy is:0.578125\n",
      "iter is:466, accuracy is:0.515625\n",
      "iter is:467, accuracy is:0.47265625\n",
      "iter is:468, accuracy is:0.49444445967674255\n"
     ]
    }
   ],
   "source": [
    "for i, batch in enumerate(train_dataloader):\n",
    "    label, data = batch\n",
    "    data = torch.tensor(data.long()).to(cfg.device)\n",
    "    label = torch.tensor(label, dtype=torch.int64).to(cfg.device)\n",
    "    pred = model_text_cls.forward(data) #模型的输出值\n",
    "    pred = torch.argmax(pred, dim=1)\n",
    "    \n",
    "    out = torch.eq(pred, label)\n",
    "    accuracy = out.sum()*1.0 / pred.size()[0]\n",
    "    print(\"iter is:{}, accuracy is:{}\".format(i, accuracy))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 此流程只是实例,要想完全做好这个项目还需自己调参"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
